var app = getApp();

Page({
  data: {
    current: 'tab1',
    tab1:true,

    mode: "scaleToFill",
    arr: [],
    indicatorDots: true,
    autoplay: true,
    interval: 2000,
    duration: 1000,  
    venueDetail:[],
    venueCoach:[],
    comments:[],
    id:''
  },
  onLoad: function (options) {
    this.setData({
      id:options.id,
    })
    var that = this
    console.log(options)
    wx.request({
      url: app.url + 'weiapp/api/getComment',
      data:{
        id:that.data.id
      },
      success:function(res)
      {
        console.log(res.data.data)
        that.setData({
          comments:res.data.data
        })
      }
    })
    wx.request({
      url: app.url + 'weiapp/api/getVenueDetail',
      data: {
        id: that.data.id
      },
      success: function (res) {
        console.log(res)
        if (res.data.code != 200) {
          wx.showToast({
            title: '错误代码:' + res.data.code,
            icon: 'none',
            duration: 5000
          })
        }
        else {
          that.setData({
            venueDetail:res.data.data
          })
        }
      }
    })
  },
  handleChange({ detail }) {
    var index = detail.key
    this.setData({
      current: detail.key
    });
    if(index == 'tab1')
    {
      this.setData({
        tab1:true,
        tab2:false,
        tab3:false
      })
    }
    else if(index == 'tab2')
    {
      this.setData({
        tab1:false,
        tab2:true,
        tab3:false
      })
      this.venueCoach(this.data.id)
    }
    else if(index == 'tab3')
    {
      this.setData({
        tab1:false,
        tab2:false,
        tab3:true
      })
    }
  },
  venueCoach(id){
    var that = this
    wx.request({
      url: app.url + 'weiapp/api/getVenueCoach',
      data:{
        id:id
      },
      success: function (res) {
        if (res.data.code != 200) {
          wx.showToast({
            title: '错误代码:' + res.data.code,
            icon: 'none',
            duration: 5000
          })
        }
        else {
          console.log(res.data.data)
          that.setData({
            venueCoach: res.data.data
          })
        }
      }      
    })
  },
  goCoachGoods(even){
    console.log(even)
    wx.navigateTo({
      url: '../coach_goods/coach_goods?id=' + even.currentTarget.dataset.coach_id,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  goMap(e) {
    wx.getLocation({//获取当前经纬度
      type: 'wgs84', //返回可以用于wx.openLocation的经纬度，官方提示bug: iOS 6.3.30 type 参数不生效，只会返回 wgs84 类型的坐标信息  
      success: function (res) {
        wx.openLocation({//​使用微信内置地图查看位置。
          latitude: parseFloat(e.currentTarget.dataset.lat),//要去的纬度-地址
          longitude: parseFloat(e.currentTarget.dataset.lng),//要去的经度-地址
          name: e.currentTarget.dataset.address,
          address: e.currentTarget.dataset.address
        })
      }
    })
  },
  scan: function () {
    var that = this;
    var show;
    wx.scanCode({
      success: (res) => {
        this.show = "结果:" + res.result + "二维码类型:" + res.scanType + "字符集:" + res.charSet + "路径:" + res.path;
        that.setData({
          show: this.show
        })
        console.log(res.path + '/user_id/' + wx.getStorageSync('uid'));
        wx.request({
          url: res.path + '/user_id/' + wx.getStorageSync('uid'),
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              wx.navigateTo({
                url: '../scan_qr_suc/scan_qr_suc?venue_id=' + res.data.data.venue_id + '&user_id=' + res.data.data.user_id,
              })
            }
            if (res.data.code == 100019) {
              wx.navigateTo({
                url: '../scan_qr_err/scan_qr_err?code=1&msg=' + res.data.msg,
              })
            }
            if (res.data.code == 100025) {
              wx.navigateTo({
                url: '../scan_qr_err/scan_qr_err?code=2&msg=' + res.data.msg,
              })
            }
            if (res.data.code == 100026) {
              wx.navigateTo({
                url: '../scan_qr_err/scan_qr_err?code=3&msg=' + res.data.msg,
              })
            }
          }
        })

      }
    })
  },
  //图片点击事件
  imgYu: function (event) {
    console.log(event)
    var src = event.currentTarget.dataset.src;//获取data-src
    var imgList = this.data.venueDetail.intro_img;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  }      
});